package controllers;


import java.util.List;

import models.logs.LoginLogs;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import utils.MengException;
import utils.StringUtils;

public class LoginLogsController extends Controller {

	public static Result createLoginLogs() {

		JsonNode json = request().body().asJson();
		LoginLogs ll  = Json.fromJson(json, LoginLogs.class);

		try {
			boolean flag = LoginLogs.getInstances().createLoginLogs(ll);
			return ok(StringUtils.toJson("status", flag));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}

	public static Result getLoginLogs(String username) {
		
		try {
			LoginLogs ll = LoginLogs.getInstances().getLoginLogs(username);
			return ok(Json.toJson(ll));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}

	public static Result getLastLoginLogs(String username) {
		
		try {
			LoginLogs ll = LoginLogs.getInstances().getLastLoginLogs(username);
			return ok(Json.toJson(ll));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}
	
	public static Result getLoginLogsPage(String username, int page, int size) {
		
		try {
			List<LoginLogs> ll = LoginLogs.getInstances().getLoginLogs(username,page,size);
			ObjectNode json = Json.newObject();
			json.put("login_logs", Json.toJson(ll));
			return ok(json);
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}
}
